﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebSite.Controllers;

namespace WebSite.Factories
{
    public class HelloWorldControllerFactory : DefaultControllerFactory
    {
        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            try
            {
                if (controllerType == null)
                    return base.GetControllerInstance(requestContext, controllerType);
            }
            catch(HttpException ex)
            {
                if (ex.GetHttpCode() == 404)
                {
                    ErrorController errorController = new ErrorController();
                    errorController.InvokeHttp404(requestContext.HttpContext);
                    return errorController;
                }
                else
                    throw ex;
            }

            return base.GetControllerInstance(requestContext, controllerType);
        }
    }
}